//用户退出登陆
$('#signout-enter').click(function() {
	$.post("",
		function(json) {
			if(json.result == "success") {
				window.location.href = "index.jsp";
			}
		}
	);
});

$('#navbar-user').click(function() {
	$('#modal-user-title').text($('#navbar-user').text());
});

//检查指定文本框的输入情况
function checkInput() {
	var tips = arguments[arguments.length - 1];
	var alert_place = arguments[arguments.length - 2];
	for (var i = 0; i < arguments.length - 2; ++i) {
		if ($(arguments[i]).val() == "") {
			showAlert(alert_place, "alert-danger", tips);
			return false;
		}
	}
	return true;
}
	
//在id处嵌入警告框
function showAlert(id, statusClass, tip) {
	$(id).empty();
	var alert = "<div class=\"alert "+statusClass+" alert-dismissible\">"
		+"<a class=\"close\" data-dismiss=\"alert\"><span>&times;</span></a>"+tip+"</div>";
	$(id).append(alert);
}

//在id处制作一个分页
function makePaginator(id, pageNow, pageNum, pageTotal) {
    var options = {
        bootstrapMajorVersion:3,
      	currentPage: pageNow,
        numberOfPages: pageNum,
      	totalPages: pageTotal
    };
    $(id).bootstrapPaginator(options);
}

//modal.delete.jsp-------------------------------------------------------------------------------------
//删除确认
$('#modal-delete-confirm').click(function() {
	setTimeout(function() {
		$.post(deleteURL,
			{
				ID: deleteID
			},
			function(json) {
				if(json.result == "success") {
					showAlert(deleteAlert, "alert-success", deleteID+" 删除成功");
				} else {
					showAlert(deleteAlert, "alert-danger", json.result);
				}
			}
		);
	}, 1);
	
});

//modal.info.user.jsp----------------------------------------------------------------------------------
//用于获取用户信息中 用户性别 复选框的值
var updateUserSex = "";
$('#update-user-sex label').click(function() {
	updateUserSex = $(this).text();
});

//实现下拉选择框的效果
$('#user-list-school li').click(function() {
	$('#update-user-school').text($(this).text());
});
$('#user-list-class li').click(function() {
	$('#update-user-class').text($(this).text());
});

//保存用户信息
$('#update-user-info').click(function() {
	$.post("",
		{
			ID: $('#update-user-ID').val(),
			email: $('#update-user-email').val(),
			nickname: $('#update-user-nickname').val(),
			signature: $('#update-user-signature').val(),
			name: $('#update-user-name').val(),
			sex: updateUserSex,
			school: $('#update-user-school').val(),
			class: $('#update-user-class').val()
		}, 
		function(json) {
			if(json.result == "success") {
				showAlert('#alert-user-info', "alert-success", "信息保存成功");
			} else {
				showAlert('#alert-user-info', "alert-danger", json.result);
			}
		}
	)
});

//保存用户密码
$('#update-user-secure').click(function() {
	if (checkInput('#update-user-password-old', '#update-user-password', '#update-user-password-check', '#alert-user-secure', "请输入全部信息")) {
		if ($('#update-user-password').val().length < 6) {
			showAlert('#alert-user-secure', "alert-danger", "新密码应大于6位");
			return;
		}
		if ($('#update-user-password').val() != $('#update-user-password-check').val()) {
			showAlert('#alert-user-secure', "alert-danger", "两次输入不一致");
			return;
		}
		$.post("",
			{
				ID: $('#update-user-ID').val(),
				oldPassword: $('#update-user-password').val(),
				password: $('#update-user-password').val()
			},
			function(json) {
				if(json.result == "success") {
					showAlert('#tab-user-secure', "alert-success", "密码更改成功");
				} else {
					showAlert('#tab-user-secure', "alert-danger", json.result);
				}
			}
		);
	}
});
//保存用户密码, 管理员
$('#update-user-secure-admin').click(function() {
	if (checkInput('#update-user-password-admin', '#update-user-password-check-admin', '#alert-user-secure-admin', "请输入全部信息")) {
		if ($('#update-user-password-admin').val().length < 6) {
			showAlert('#alert-user-secure-admin', "alert-danger", "密码应大于6位");
			return;
		}
		if ($('#update-user-password-admin').val() != $('#update-user-password-check-admin').val()) {
			showAlert('#alert-user-secure-admin', "alert-danger", "两次输入不一致");
			return;
		}
		$.post("",
			{
				ID: $('#update-user-ID').val(),
				password: $('#update-user-password-admin').val()
			},
			function(json) {
				if(json.result == "success") {
					showAlert('#tab-user-secure-admin', "alert-success", "密码更改成功");
				} else {
					showAlert('#tab-user-secure-admin', "alert-danger", json.result);
				}
			}
		);
	}
});

//modal.info.problem.jsp-------------------------------------------------------------------------------
//实现下拉选择框的效果
$('#modal-problem-list-point li').click(function() {
	$('#update-problem-point').text($(this).text());
});

//tab.man.problem.jsp---------------------------------------------------------------------------------
//点击题目列表时，提取列表中ID和题目名并放置在 题目信息 和 删除确认 中
function prepareProblemInfo(tr) {
	var problemID = tr.firstChild.innerText,
		problemName = (tr.childNodes)[1].innerText;
		deleteID = problemID;	//全局变量deleteID
		deleteURL = "";	//全局变量deleteURL，在这里改题目删除的地址
		deleteAlert = '#alert-problem';	//全局变量deleteAlert
	$('#modal-problem-title').text(problemID+" "+problemName);
	$('#modal-delete-title').text("确认删除 "+problemID+problemName+"？");
}

//实现下拉选择框的效果
$('#problem-list-point li').click(function() {
	$('#problem-add-point').text($(this).text());
});
	
//测试内容
makePaginator('#problem-paginator', 1, 10, 10);

//tab.man.task.jsp-------------------------------------------------------------------------------------
//点击作业列表时，提取列表中ID和作业名并放置在 作业信息 和 删除确认 中
function prepareTaskInfo(tr) {
	var taskID = tr.firstChild.innerText,
		taskName = (tr.childNodes)[1].innerText;
		deleteID = taskID;	//全局变量deleteID
		deleteURL = "";	//全局变量deleteURL，在这里改作业删除的地址
		deleteAlert = '#alert-task';	//全局变量deleteAlert
	$('#modal-task-title').text(taskID+" "+taskName);
	$('#modal-delete-title').text("确认删除 "+taskID+taskName+"？");
}
//点击作业题目列表时，提取列表中ID和题目名并放置在 删除确认 中
function prepareTaskProblemInfo(tr) {
	deleteID = tr.firstChild.innerText;	//全局变量deleteID
	deleteURL = "";	//全局变量deleteURL，在这里改题目删除的地址
	deleteAlert = '';	//全局变量deleteAlert，不需要，直接刷新列表就好
}

//实现下拉选择框的效果
$('#task-class-list-add li').click(function() {
	$('#task-add-class').text($(this).text());
});
$('#update-task-class-list li').click(function() {
	$('#update-task-class').text($(this).text());
});

//测试内容
makePaginator('#task-paginator', 1, 10, 10);

//tab.man.match.jsp------------------------------------------------------------------------------------
//点击比赛列表时，提取列表中ID和比赛名并放置在 比赛信息 和 删除确认 中
function prepareMatchInfo(tr) {
	var matchID = tr.firstChild.innerText,
		matchName = (tr.childNodes)[1].innerText;
		deleteID = matchID;	//全局变量deleteID
		deleteURL = "";	//全局变量deleteURL，在这里改比赛删除的地址
		deleteAlert = '#alert-match';	//全局变量deleteAlert
	$('#modal-match-title').text(matchID+" "+matchName);
	$('#modal-delete-title').text("确认删除 "+matchID+matchName+"？");
}
//点击比赛题目列表时，提取列表中ID和题目名并放置在 删除确认 中
function prepareMatchProblemInfo(tr) {
	deleteID = tr.firstChild.innerText;	//全局变量deleteID
	deleteURL = "";	//全局变量deleteURL，在这里改题目删除的地址
	deleteAlert = '';	//全局变量deleteAlert，不需要，直接刷新列表就好
}

//测试内容
makePaginator('#match-paginator', 1, 10, 10);